package com.ysxsoft.wdg.ui.activity.order;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.EmsResponse;
import com.ysxsoft.wdg.bean.response.CollectShopResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.widget.EmsLineView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 物流详情
 */
public class EmsActivity extends BaseActivity {

    @BindView(R.id.emsLineView)
    EmsLineView emsLineView;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.emsName)
    TextView emsName;
    @BindView(R.id.tel)
    TextView tel;
    @BindView(R.id.bh)
    TextView bh;

    private String emsTypeCode = "", emsNo = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ems;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("物流详情");

        emsNo = getIntent().getStringExtra("emsNo");
        emsTypeCode = getIntent().getStringExtra("emsTypeCode");

        emsLineView.setLineColor(Color.parseColor("#cccccc"));
        emsLineView.setIsFirst(true);//是否是第一个选择
        emsLineView.setDrawEndPoint(false);
        emsLineView.setNormalIsEmpty(false);
        emsLineView.requestLayout();
        getDetail();
    }

    private View createView(int i, String t1, String t2) {
        View v = View.inflate(EmsActivity.this, R.layout.view_ems, null);
        TextView title = v.findViewById(R.id.title);
        TextView time = v.findViewById(R.id.content);
        if (i == 0) {
            title.setTextColor(getResources().getColor(R.color.black3));
            time.setTextColor(getResources().getColor(R.color.black3));
        } else {
            title.setTextColor(getResources().getColor(R.color.gray_c));
            time.setTextColor(getResources().getColor(R.color.gray_c));
        }
        title.setText(t1);
        time.setText(t2);
        return v;
    }

    @Override
    protected void setListener() {

    }

    private void getDetail() {
        if (emsNo == null || emsTypeCode == null) {
            ToastUtils.showToast("单号不正确！");
            return;
        }
        OkHttpUtils.post()
                .url(UrlHost.EMS_DETAIL)
                .addParams("code", emsNo)
                .addParams("type", emsTypeCode)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    if (response != null) {
                        EmsResponse emsResponse = JsonUtils.parseByGson(response, EmsResponse.class);
                        if (emsResponse == null) {
                            return;
                        }
                        if (CallbackCode.SUCCESS.equals(emsResponse.getCode())) {
                            ToastUtils.showToast(emsResponse.getMs());
                            if (emsResponse.getData() != null) {
                                emsName.setText(emsResponse.getData().getShipperCode());
                                tel.setText("手机号暂缺");//TODO:暂缺
                                bh.setText(emsResponse.getData().getLogisticCode());
                                List<EmsResponse.DataBean.ListBean> list = emsResponse.getData().getList();
                                for (int i = 0; i < list.size(); i++) {
                                    EmsResponse.DataBean.ListBean bean = list.get(i);
                                    emsLineView.addView(createView(i, bean.getAcceptStation(), bean.getAcceptTime()));
                                }
                                emsLineView.requestLayout();
                            }
                        } else {
                            ToastUtils.showToast(emsResponse.getMs());
                        }
                    } else {
                    }
                } catch (Exception e) {
                    ToastUtils.showToast("物流信息不完善，请检查单号是否正确！");
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    private class EmsOrder {
        private String name;
        private String content;

        public EmsOrder(String name, String content) {
            this.name = name;
            this.content = content;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
}
